package com.snopy.concurrency.cyclicbarrier;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

/**
 * @author snopy
 * @version 1.0.0
 * @classname DemoCyclicBarrier
 * @description
 * @email 77912204@qq.com
 * @date 2022/06/30 16:46
 */
@Slf4j
public class DemoCyclicBarrier {


    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        CyclicBarrier cyclicBarrier = new CyclicBarrier(3,()->{
            log.debug("start summary...");
        });

        for (int i = 0; i < 3; i++) {
            executorService.submit(()->{
                try {
                    log.debug("task1...");
                    TimeUnit.SECONDS.sleep(1);
                    cyclicBarrier.await();
                } catch (InterruptedException | BrokenBarrierException e) {
                    e.printStackTrace();
                }
            });
            executorService.submit(()->{
                try {
                    log.debug("task2...");
                    TimeUnit.SECONDS.sleep(3);
                    cyclicBarrier.await();
                } catch (InterruptedException | BrokenBarrierException e) {
                    e.printStackTrace();
                }
            });
            executorService.submit(()->{
                try {
                    log.debug("task3...");
                    TimeUnit.SECONDS.sleep(2);
                    cyclicBarrier.await();
                } catch (InterruptedException | BrokenBarrierException e) {
                    e.printStackTrace();
                }
            });
        }
        executorService.shutdown();
    }
}
